package com.action;

import cn.hutool.core.io.FileUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.bean.ResponseBean;
import com.pojo.Config;
import com.pojo.Zixun;
import com.service.ConfigService;
import com.service.ZixunService;
import com.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 学校关键内容配置操作类
 * author:liao
 */
@RestController
@RequestMapping("/config")
public class ConfigAction {
    @Value("${upload.dir}")
    private String upload ;

    @Value("${upload.tmp.dir}")
    private String tmp ;
    @Autowired
    private ConfigService configService;
    /**
     * 查询(所有)配置内容
     * @param name
     * @return
     */
    @RequestMapping("/getall")
    public ResponseBean getZixunPage(String name){
        QueryWrapper<Config> qw = new QueryWrapper();
        if(StringUtil.isNotNull(name))
            qw.like("name",name);
        List list =configService.list(qw);
        return new ResponseBean(0, list.size(), "成功", list);
    }


    /**
     * 添加配置信息
     * @param config
     * @return
     */
    @PostMapping("/add")
    public ResponseBean add(@RequestBody Config config){
        if(StringUtil.isNull(config.getName())) {
            return ResponseBean.error("请填写配置属性") ;
        }
        if(StringUtil.isNull(config.getValue())) {
            return ResponseBean.error("请填写配置内容") ;
        }
        config.setIs_show(1);
        if(config.getType()==2)
            FileUtil.copy(tmp + config.getValue(), upload, true) ;
       boolean f= configService.save(config);
       if(f){
           return ResponseBean.ok("添加成功");
       }else{
           return ResponseBean.error("添加失败");
       }
    }
    /**
     * 是否显示配置信息
     * @param config
     * @return
     */
    @PostMapping("/update")
    public ResponseBean update(@RequestBody Config config){
        if(config.getIs_show()==1)
            config.setIs_show(0);
        else
            config.setIs_show(1);
        boolean f= configService.updateById(config);
        if(f){
            return ResponseBean.ok("修改成功");
        }else{
            return ResponseBean.error("修改失败");
        }
    }
    /**
     * 修改配置信息
     * @param config
     * @return
     */
    @PostMapping("/updateinfo")
    public ResponseBean updateinfo(@RequestBody Config config){
        Config old = configService.getById(config.getId());
        if(config.getType() == 2) {
            if (old.getValue()!=null &&!old.getValue().equals(config.getValue())) {  // 如果图片内容不一样
                FileUtil.copy(tmp + config.getValue(), upload, true);
            }else{
                FileUtil.copy(tmp + config.getValue(), upload, true);
            }
        }
        boolean f= configService.updateById(config);
        if(f){
            return ResponseBean.ok("修改成功");
        }else{
            return ResponseBean.error("修改失败");
        }
    }
    /**
     * 查询配置详情
     * @param id
     * @return
     */
    @RequestMapping("/getone")
    public ResponseBean getone(String id){

        Config config = configService.getById(id);
        if(config!=null){
            return new ResponseBean(200,"成功",config);
        }else{
            return ResponseBean.error("查询失败");
        }
    }
}
